package com.nulldev.util.data.Date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.nulldev.util.JVM.JVM;

public class Dates {

	public static Date time(final long mills) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(mills);
		return calendar.getTime();
	}

	public static Date get() {
		final Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	private static final SimpleDateFormat sdf = new SimpleDateFormat("DD/MM/YYYY");

	public static Date makeDDMMYYYY(final String string) {
		if (string == null || string.isEmpty())
			throw new IllegalArgumentException("string == null!");
		try {
			return sdf.parse(string);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static double now() {
		return System.currentTimeMillis();
	}

	public static String nowISO() {
		if (JVM.version() >= 1.8f) {
			return java.time.ZonedDateTime.now(java.time.ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT);
		}else {
			final TimeZone tz = TimeZone.getTimeZone("UTC");
			final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
			df.setTimeZone(tz);
			return df.format(new Date());
		}
	}
}
